<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>itbyteset</title>
		<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="js/mqttws31.min.js"></script>
		<script type="text/javascript" src="js/itbyteset.js"></script>
	</head>

	<body id="body">
		<div rtdata="level1/aa/tag1_num">itbyteset测试1</div>
		<div rtdata="level1/aa/tag2_num">itbyteset测试2</div>
		<script>
			var sun = 0;
			var host = "sealush.tpddns.cn";
			var port = 8083;
			var clientId = "example2";
			var user = "";
			var password = "";
			var dest = "level1/+/+";
			var itbytes = new Itbytes();
			var onConnect = function(frame) { //mqtt连接成功回调
				client.subscribe(dest);
				console.log("mqtt connect to [%s] success", dest)
			};
			var onConnectionLost = function() { //mqtt连接丢失回调
				console.log("mqtt [%s] disconnect", dest)
			};
			var onFailure = function() { //mqtt连接失败回调
				console.log("mqtt connect to [%s] fail", dest)
			};
			var transform = function(msg) {
				//!!!!!!!!!!!!!固定格式，两个属性topic和payload
				return {
					topic: msg.destinationName,
					payload: msg.payloadBytes
				}
			}
			itbytes.transform = transform; //=====================需要转化的格式=============================
			var client = new Paho.MQTT.Client(host, Number(port), clientId);
			client.onConnect = onConnect; //设置设置连接成功回调
			client.onMessageArrived = itbytes.callBack; //设置数据推送回调
			client.onConnectionLost = onConnectionLost; //设置连接丢失回调
			client.connect({ //开始连接
				userName: user,
				password: password,
				onSuccess: onConnect,
				onFailure: onFailure
			});
			itbytes.Interval(200);
			itbytes.autodom(true);
			console.log(itbytes.getAllTag());
			$("#body").append("<div rtdata = 'level1/aa/tag3_num' > itbyteset测试3 </div>");
			itbytes.DOMchanged();
			console.log(itbytes.getAllTag());

			function doSomeThingTag0(rtdata) {
				itbytes.usetag(doSomeThingTag0, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			function doSomeThingTag1(rtdata) {
				itbytes.usetag(doSomeThingTag1, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			function doSomeThingTag2(rtdata) {
				itbytes.usetag(doSomeThingTag2, "level1/aa/tag0_num");
				var tag2_v = itbytes.getValue(rtdata);
				if(tag2_v) {
					sun += tag2_v
					console.log(sun);
				}
			}

			doSomeThingTag0();
			doSomeThingTag1();
			doSomeThingTag2();
		</script>
	</body>

</html>